package com.ZengQi.event.util;

import com.ZengQi.event.entity.Admin;
import com.ZengQi.event.entity.Company;
import com.ZengQi.event.entity.Topic;
import org.apache.commons.lang3.builder.ToStringExclude;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;


/**
 * 数据库的查询操作工具类
 */
public class SearchUtils {
    public static <T> List<T> searchForList(Class<T> clazz,String sql,Object...args){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn=JDBCUtils.getConnection();
            ps=conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
             rs=ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            ArrayList<T>list=new ArrayList<T>();
            while (rs.next()){
                T t=clazz.newInstance();
                for(int i=0;i<columnCount;i++){
                    Object columValue = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columValue);
                }
                list.add(t);

            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
finally {
            JDBCUtils.close(rs,ps,conn);
        }
        return null;
    }

    public static <T> List<T> searchForList(Class<T> clazz,String sql){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn=JDBCUtils.getConnection();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            ArrayList<T>list=new ArrayList<T>();
            while (rs.next()){
                T t=  clazz.newInstance();
                for(int i=0;i<columnCount;i++){
                    Object columValue = rs.getObject(i + 1);
//利用反射获取类的成员变量名并对应赋值
                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columValue);
                }
                list.add(t);

            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtils.close(rs,ps,conn);
        }
        return null;
    }


}
